<template>
  <div class="edit-index">
    <div class="right">
      <CommonTemplate style="background-color: #f6f7f9">
        <template #nav>
          <Navbar class="navbar"></Navbar>
        </template>
        <template #body>
          <router-view></router-view>
        </template>
      </CommonTemplate>
    </div>
  </div>
</template>
<script setup lang="ts">
import { onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import 'element-plus/theme-chalk/src/message.scss'

import CommonTemplate from './components/CommonTemplate.vue'
import Navbar from './components/ModuleNavbar.vue'

import { initShowLogicEngine } from '@/management/hooks/useShowLogicEngine'

const store = useStore()
const router = useRouter()
const route = useRoute()

onMounted(async () => {
  store.commit('publishform/setFormId', route.params.id)

  try {
    await store.dispatch('publishform/init')
    await initShowLogicEngine(store.state.publishform.schema.logicConf.showLogicConf || {})
    if (store.state?.editform?.schema?.baseConf?.adminUser?.length) {
      let creatorId = store.state?.editform?.schema?.metaData?.creatorId || ''
      let userId = store.state?.editform?.schema?.baseConf?.adminUser.map((item: any) => item.value) || []
      if (userId.length > 0 && !userId.includes((window as any).$app?.user?.userId) && creatorId !== (window as any).$app?.user?.userId) {
        // ElMessage.error('您没有权限访问该页面')
        router.replace({ name: 'FormsPublishPage' })
      }
    }
  } catch (err: any) {
    ElMessage.error(err.message)

    setTimeout(() => {
     router.replace({ name: 'forms' })
    }, 1000)
  }
})
</script>
<style lang="scss" scoped>
.edit-index {
  height: 100%;
  width: 100%;
  overflow: hidden;

  .left {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 100;
  }

  .right {
    min-width: 1160px;
    height: 100%;
    padding-left: 0px;
    overflow: hidden;
  }

  .navbar {
    border-bottom: 1px solid #e7e9eb;
  }
}
</style>
